package com.edu.hre.common.utils;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.WebApplicationContextUtils;


// Class gọi bean trong applicationContext
public class BeanFinder extends ContextLoaderListener {
	private static ApplicationContext ctx = null;

	 
	public void contextInitialized(javax.servlet.ServletContextEvent event) {
		// CommonUtils.println(CommonUtils.getSourceCodeLocation()); // TODO the code logic is temporarily commented out here because an exception has been thrown from!
		ctx = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
		
		// load dynamic config
		DynamicConfig.loadDynamicConfig();
	} 

	public static Object getBean(String className) {
		if(ctx != null){
			return ctx.getBean(className);
		}
		return null;
	} 
	
	public static <X> X getBean(String className, Class<X> clazz) {
		if(ctx != null){
			return (X) ctx.getBean(className, clazz);
		}
		return null;
	}
}
